package zstrconv

import (
	"net/url"
	"strconv"
	"time"
)

// StrInsert 在字符串中插入字符串
func StrInsert(s string, insertIndex int, value string) (str string) {
	if insertIndex > len(s) {
		str = s + value
		return
	}
	str1 := s[0:insertIndex] + value
	str2 := s[insertIndex+1 : len(s)-insertIndex]
	str = str1 + str2
	return
}

func StrDelete(s string, sInd int, eInd int) string {
	if sInd > len(s) {
		return s
	}
	tmp := append([]byte(s[:sInd]), []byte(s[eInd+1:])...)
	return string(tmp)
}

// UrlToUrlEncode urlEncode编码
func UrlToUrlEncode(path string) string {
	return url.QueryEscape(path)
}

// StrToInt 字符串转int
func StrToInt(strNumber string, value interface{}) (err error) {
	var number interface{}
	number, err = strconv.ParseInt(strNumber, 10, 64)
	switch v := number.(type) {
	case int64:
		switch d := value.(type) {
		case *int64:
			*d = v
		case *int:
			*d = int(v)
		case *int16:
			*d = int16(v)
		case *int32:
			*d = int32(v)
		case *int8:
			*d = int8(v)
		}
	}
	return
}

// StrToUint 字符串转uint
func StrToUint(strNumber string, value interface{}) (err error) {
	var number interface{}
	number, err = strconv.ParseUint(strNumber, 10, 64)
	switch v := number.(type) {
	case uint64:
		switch d := value.(type) {
		case *uint64:
			*d = v
		case *uint:
			*d = uint(v)
		case *uint16:
			*d = uint16(v)
		case *uint32:
			*d = uint32(v)
		case *uint8:
			*d = uint8(v)
		}
	}
	return
}

// StrToFloat 字符串转float
func StrToFloat(strNumber string, value interface{}) (err error) {
	var number interface{}
	number, err = strconv.ParseFloat(strNumber, 64)
	switch v := number.(type) {
	case float64:
		switch f := value.(type) {
		case *float64:
			*f = v
		case *float32:
			*f = float32(v)
		}
	}
	return
}

// HexStrToUintArray 16进制字符串转uint数组,bitSize:数值的位数(取值0,8,16,32,64) 0=uint
func HexStrToUintArray(strData string, start, bitSize int) (num []interface{}, err error) {
	var n uint64
	flag := 8 //初始化标识为32位,8个字符
	if bitSize != 0 {
		flag = bitSize / 4
	}
	strLen := len(strData)            //字符串长度
	arrLen := (strLen - start) / flag //计算出数组最大长度
	num = make([]interface{}, arrLen)
	switch bitSize {
	case 0:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseUint(strData[start:start+flag], 16, bitSize)
			num[i] = uint(n)
			start += flag
		}
	case 8:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseUint(strData[start:start+flag], 16, bitSize)
			num[i] = uint8(n)
			start += flag

		}
	case 16:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseUint(strData[start:start+flag], 16, bitSize)
			num[i] = uint16(n)
			start += flag

		}
	case 32:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseUint(strData[start:start+flag], 16, bitSize)
			num[i] = uint32(n)
			start += flag
		}
	case 64:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseUint(strData[start:start+flag], 16, bitSize)
			num[i] = n
			start += flag
		}
	}
	return
}

// HexStrToIntArray 16进制字符串转int数组,bitSize:数值的位数(取值0,8,16,32,64) 0=int
func HexStrToIntArray(strData string, start, bitSize int) (num []interface{}, err error) {
	var n int64
	flag := 8 //初始化标识为32位,8个字符
	if bitSize != 0 {
		flag = bitSize / 4
	}

	strLen := len(strData)
	arrLen := (strLen - start) / flag

	num = make([]interface{}, arrLen)

	switch bitSize {
	case 0:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseInt(strData[start:start+flag], 16, bitSize)
			num[i] = int(n)
			start += flag
		}
	case 8:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseInt(strData[start:start+flag], 16, bitSize)
			num[i] = int8(n)
			start += flag
		}
	case 16:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseInt(strData[start:start+flag], 16, bitSize)
			num[i] = int16(n)
			start += flag
		}
	case 32:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseInt(strData[start:start+flag], 16, bitSize)
			num[i] = int32(n)
			start += flag
		}
	case 64:
		for i := 0; strLen-start >= flag; i++ {
			n, err = strconv.ParseInt(strData[start:start+flag], 16, bitSize)
			num[i] = n
			start += flag
		}
	}
	return
}

// ToLocalTime 字符串转成本地时间
func ToLocalTime(t string) time.Time {
	loc, _ := time.LoadLocation("Local")
	sTime, _ := time.ParseInLocation(time.DateTime, t, loc)
	return sTime
}

// IsTime 字符串是否是时间（yyyyMMddHHmm ss）
func IsTime(s string) bool {
	_, e := time.Parse(time.DateTime, s)
	if e == nil {
		return true
	}
	return false
}
